'use strict';

app.controller('UpdatePwdController', ['$scope', '$translate', 'RESTService', function($scope, $translate, RESTService) {
	$scope.currUser = {
		realName: "",
		hosp_Cn_Name: ""
	}
	var urladdress = "/rest/user/getUser?";
	RESTService.getResponse(urladdress, true).then(function(response) {
		if(response.code == 0) {
			$scope.currUser.realName = response.data[0].realName;
			if(response.data[0].isDoctor) {
				urladdress = "/rest/doctor/getDoctor?userId=" + response.data[0].userId;
				RESTService.getResponse(urladdress).then(function(res) {
					$scope.currUser.hosp_Cn_Name = res[0]._hosp.hosp_Cn_Name;
				});
			}
		}
	});
	
	$scope.updatePwdError = null;
	$scope.user = {};
	$scope.updatePwd = function () {
		if($scope.user.newPassword != $scope.user.confirmPassword) {
    		$scope.authError = $translate.instant("user.passwordMatch");
    		return;
    	}
		var urladdress = "/rest/user/updatePassword?";
		RESTService.postResponse(urladdress,{oldPassword : $scope.user.oldPassword, newPassword : $scope.user.newPassword}, true)
			.then(function(response) {
			console.log('updatePassword====', response);
			if(response.code == 0){
				$scope.updatePwdError = $translate.instant("user.passwordSuccess");
				$scope.user = {};
			}
		}, function(data2) {
			$scope.updatePwdError = data2.error;
		});
	};
}]);

app.controller('ProfileController', ['$scope', 'RESTService', function($scope, RESTService) {
	$scope.user = {
		userId: -1,
        userName: '',
        realName: '',
        mobile_Phone: '',
        email: '',
        userType_Id: 1,
        userType: '',
        hosp_Id: null,
        hosp: '',
        dept_Id: null,
        dept: '',
        doctQuali_Id: null,
        doctQuali: '',
        doctor_Intro: ''
	};
	var urladdress = "/rest/user/getUser?";
	RESTService.getResponse(urladdress).then(function(response) {
		console.log('getUser====', response);
		console.log('getUser====', response[0]._userType.userType_Name);
		$scope.user.userId = response[0].userId;
		$scope.user.userName = response[0].userName;
		$scope.user.realName = response[0].realName;
		$scope.user.mobile_Phone = response[0].mobile_Phone;
		$scope.user.email = response[0].email;
		$scope.user.userType_Id = response[0].userType_Id;
		$scope.user.userType = response[0]._userType.userType_Name;
		//if(response.userType_Id == 1) {
			urladdress = "/rest/doctor/getDoctor?userId=" + response[0].userId;
			RESTService.getResponse(urladdress).then(function(res) {
				console.log('urladdress====', res);
				if(res != null && res.length > 0) {
					$scope.user.doctor_Intro = res[0].doctor_Intro;
					$scope.user.hosp_Id = res[0].hosp_Id;
					$scope.user.hosp = res[0]._hosp.hosp_Cn_Name;
					$scope.user.dept_Id = res[0].dept_Id;
					$scope.user.dept = res[0]._dept.dept_Cn_Name;
					$scope.user.doctQuali_Id = res[0].doctQuali_Id;
					$scope.user.doctQuali = res[0]._doctQuali.doctQuali_Cn_Name;
				}
			}, function(data2) {
				$scope.authError = data2.error;
			});
		//}
		
	}, function(data4) {
		$scope.authError = data4.error;
	});
}]);

